<?php require('./head.php');?>
<?php
require('./db.php');
function func_response($str = '')
{
    echo $str;exit;
}
function func_request($str){
    return isset($_REQUEST[$str]) ? $_REQUEST[$str] : '';
}
$template = func_request('template');
if(is_numeric($template) && $template > 0) {
    $sql = "SELECT * FROM `template` where k='{$template}'";
    $res = Database::query($sql);
    $content = html_entity_decode($res[0]['v']);

}
$sql = "SELECT * FROM `template` order by k";
$res = Database::query($sql);
/*
$sql = "SELECT * FROM `template` order by k";
$res = Database::query($sql);
if (empty($res)) {
    func_response("当前无模板");
}*/

?>
<div style="max-width: 1000px;margin: auto;">
    <form action="save.php" method="post">
        <div style="margin: 10px auto;text-align: center;">
            <a href="./list.php">模板列表</a>
            模板编号 <?=$template > 0 ? $template : ''?>
            <input style="<?=$template > 0 ? 'display:none;' : ''?>" type="text" name="template" value="<?=$template  ?>">
            <input type="hidden" name="add" value="<?=$template > 0 ? 0 : 1  ?>">
            <input type="submit" style="margin: auto;" value="保存模板">
        </div>
        <textarea placeholder="" id='content' name="content" style="height: 450px;width: 100%;border: 1px solid black" oninput="showTemplate()"><?=$content ?></textarea>
    </form>
    <style>
        .itemCopy{
            padding: 3px 5px;
        }
    </style>
    <div style="margin: 10px auto;text-align: center;">
        <button type="button" data-clipboard-text="[头像_URL]" class="itemCopy">[头像_URL]</button>
        <button type="button" data-clipboard-text="[QQ号码]" class="itemCopy">[QQ号码]</button>
        <button type="button" data-clipboard-text="[对方昵称]" class="itemCopy">[对方昵称]</button>
        <button type="button" data-clipboard-text="[随机数]" class="itemCopy">[随机数]</button>
        <button type="button" data-clipboard-text="[随机字符]" class="itemCopy">[随机字符]</button>
        <button type="button" data-clipboard-text="[随机一句]" class="itemCopy">[随机一句]</button>
        <button type="button" data-clipboard-text="[加群_URL]" class="itemCopy">[加群_URL]</button>
        <button type="button" data-clipboard-text="[当前时间]" class="itemCopy">[当前时间]</button>
    </div>
    <div style="background: #fff; max-width: 800px;margin-top: 10px;box-shadow: 0 0 6px 4px #ccc;margin: 10px auto;padding: 5px;" id="show"></div>

</div>
<script>
    var show = document.getElementById('show');
    var content = document.getElementById('content')
    var template = document.getElementById('template')
    function HTMLDecode(text) {
        var temp = show;
        temp.innerHTML = text;
        var output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    }
    function saveTemplate() {

        postInfos("./save.php","add=<?=($template > 0 ? 0 : 1)?>&template=" + template.value +  "&content=" + content.value, function(res){

            if(res.indexOf("success") !== -1) {
                alert('保存成功')
                parent.window.opener=null;parent.window.open('','_self');parent.window.close();
            } else {
                alert(res)
            }
        });

    }

    function showTemplate(){
        show.innerHTML = content.value
    }
    showTemplate()
    function postInfos(URL,postStr,functions,s){
        var xmlHttp = xmlInit();
        xmlHttp.open("POST", URL, true);
        xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
        xmlHttp.setRequestHeader("Content-TYPE","application/x-www-form-urlencoded");
        xmlHttp.send(postStr);
        xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                var pReturn="";
                pReturn=xmlHttp.responseText;
                functions(pReturn,s);
            }
        }
    }
    function xmlInit(){
        var xmlHttp = false;
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp =  new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                xmlHttp =  false;
            }
        }
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
            xmlHttp =  new XMLHttpRequest();
        }
        return xmlHttp;
    }
</script>
<script type="text/javascript" src="./clipboard.js"></script>
<script>
    var clipboard = new Clipboard('.itemCopy');
    clipboard.on('success', function(e) {
        console.log(e);
    });
</script>
